package cn.rkylin.oms.salespromotion.rules.util.stretegy.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.order.domain.OrderSku;
import cn.rkylin.oms.order.vo.OrderVO;
import cn.rkylin.oms.salespromotion.rules.domain.SalesPromotionConstrainGoods;
import cn.rkylin.oms.salespromotion.rules.util.stretegy.AbstractGiftCaculateStretegy;
import cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO;

public class GoodsKindsCaculator extends AbstractGiftCaculateStretegy {

    /**
     * 获取订单商品的种类数量.
     * @see cn.rkylin.oms.salespromotion.rules.util.stretegy.AbstractGiftCaculateStretegy#getCaculateNum(cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO, cn.rkylin.oms.order.vo.OrderVO)
     */
    @Override
    protected BigDecimal getCaculateNum(SalesPromotionRuleVO ruleVO, OrderVO order) {
        BigDecimal returnValue = new BigDecimal(0);
        JSONObject ruleObject = JSONObject.parseObject(ruleVO.getPresentCaculateRule());
        String purchaseType = ruleObject.getString("purchaseType");
        if("anyGoods".equals(purchaseType)) {
            // 计算所有商品的件数
            returnValue = new BigDecimal(getKinds(order));
        }else {
            // 计算所有限制商品的件数
            returnValue = new BigDecimal(getConstrainSkuKinds(ruleVO,order));
        }
        return returnValue;
    }
    
    /**
     * getKinds:计算订单商品的种类数. <br/>
     *
     * @author zhanghao
     * @param order
     * @return
     */
    private int getKinds(OrderVO order) {
        List<OrderSku> orderGoodsList = order.getSysOrderGoodsList();
        return orderGoodsList.size();
    }
    
    /**
     * getConstrainSkuKinds:计算订单商品中限制商品的种类数. <br/>
     *
     * @author zhanghao
     * @param ruleVO
     * @param order
     * @return
     */
    private int getConstrainSkuKinds(SalesPromotionRuleVO ruleVO, OrderVO order) {
        int count = 0;
        List<OrderSku> orderGoodsList = order.getSysOrderGoodsList();
        List<SalesPromotionConstrainGoods> constrainGoodsList = ruleVO.getConstrainGoodsList();
        ArrayList<String> constraingoodsSkuCodeArray = new ArrayList<String>();
        for (SalesPromotionConstrainGoods salesPromotionConstrainGoods : constrainGoodsList) {
            constraingoodsSkuCodeArray.add(salesPromotionConstrainGoods.getRelGoodsSkuCode());
        }
        
        for (OrderSku orderSku : orderGoodsList) {
            // 如果订单商品不是赠品，且存在于限制商品列表中，则计算商品数量
            if(constraingoodsSkuCodeArray.contains(orderSku.getSkuCode())) {
                count+=1;
            }
        }
        return count;
    }

}
